function thisrender(this)
%THISRENDER   

%   Author(s): J. Schickler
%   Copyright 1988-2009 The MathWorks, Inc.
%   $Revision: 1.1.6.7 $  $Date: 2009/05/23 08:16:55 $

sz    = gui_sizes(this);
cbs   = siggui_cbs(this);
icons = load(fullfile(matlabroot,'toolbox','signal','sigtools','private','fd_icons.mat'));

hFig = figure('Position', [200 200 400 300]*sz.pixf, ...
    'Visible', 'Off', ...
    'NumberTitle', 'Off', ...
    'Tag', 'FilterManager', ...
    'Name', 'Filter Manager', ...
    'IntegerHandle', 'Off', ...
    'Menubar', 'none', ...
    'HandleVisibility', 'Off', ...
    'CloseRequestFcn', {@close_cb, this}, ...
    'Color', get(0, 'DefaultUIControlBackgroundColor'));

h.toppanel    = uipanel('Parent', hFig, 'tag', 'top');

h.close = uicontrol(hFig, ...
    'Tag', 'Close', ...
    'String', 'Close', ...
    'Callback', get(hFig, 'CloseRequestFcn'), ...
    'Style', 'Pushbutton');

h.help = uicontrol(hFig, ...
    'Tag', 'Help', ...
    'String', 'Help', ...
    'Callback', @help_cb, ...
    'Style', 'Pushbutton');

% Render the listbox which stores all the filters.
h.listbox = uicontrol(h.toppanel, ...
    'Tag', 'FilterListBox', ...
    'String', 'test', ...
    'Style', 'listbox', ...
    'Interruptible', 'Off', ...
    'BusyAction', 'Queue', ...
    'Max', 2, ...
    'Callback', {@listbox_cb, this});

h.moveup = uicontrol(h.toppanel, ...
    'Tag', 'MoveUp', ...
    'CData', icons.up, ...
    'Callback', {cbs.method, this, 'moveup'});

h.movedown = uicontrol(h.toppanel, ...
    'Tag', 'MoveDown', ...
    'CData', icons.down, ...
    'Callback', {cbs.method, this, 'movedown'});

% Render the cascade button
h.cascade = uicontrol(h.toppanel, ...
    'Tag', 'Cascade', ...
    'Style', 'Pushbutton', ...
    'String', xlate('Cascade'), ...
    'Callback', {cbs.method, this, 'cascade'});

% Render the cascade button
h.rename = uicontrol(h.toppanel, ...
    'Style', 'Pushbutton', ...
    'String', xlate('Rename'), ...
    'Callback', {cbs.method, this, 'renamefilter'});

% Render the delete button.
h.delete = uicontrol(h.toppanel, ...
    'Tag', 'Delete', ...
    'Style', 'Pushbutton', ...
    'String', xlate('Remove', '-s'), ...
    'Callback', {cbs.method, this, 'removefilter'});

% Render the fvtool button
h.fvtool = uicontrol(h.toppanel, ...
    'Tag', 'FVTool', ...
    'Style', 'Pushbutton', ...
    'String', 'FVTool', ...
    'Callback', {cbs.method, this, 'fvtool'});

h.overwrite = uicontrol(h.toppanel, ...
    'Tag', 'Overwrite', ...
    'Style', 'Check', ...
    'String', xlate('Edit current filter'), ...
    'Callback', {cbs.property, this, 'overwrite'});

% Render the current filter popup
h.currentfilter = uicontrol(hFig, ...
    'Tag', 'CurrentFilter', ...
    'Style', 'Text', ...
    'HorizontalAlignment', 'Left', ...
    'String', sprintf('%s: ', xlate('Current filter')));

wd = largestuiwidth([h.cascade h.rename h.delete h.fvtool ...
    h.moveup h.movedown])+12*sz.pixf;

% Add a layout manager for the figure
hLayout = siglayout.gridbaglayout(hFig, ...
    'VerticalGap', 5*sz.pixf, ...
    'HorizontalGap', 10*sz.pixf, ...
    'HorizontalWeights', [1 0], ...
    'VerticalWeights', [1 0]);

hLayout.add(h.toppanel, 1, [1 2], ...
    'Fill', 'Both', ...
    'TopInset', 5*sz.pixf, ...
    'BottomInset', 5*sz.pixf);

hLayout.add(h.close, 2, 1, ...
    'MinimumHeight', 20*sz.pixf, ...
    'MinimumWidth', wd, ...
    'Anchor', 'East');

hLayout.add(h.help, 2, 2, ...
    'MinimumHeight', 20*sz.pixf, ...
    'MinimumWidth', wd, ...
    'RightInset', 8*sz.pixf, ...
    'Anchor', 'East');

hLayout = siglayout.gridbaglayout(h.toppanel, ...
    'VerticalWeights', [0 1 0 0], ...
    'HorizontalWeights', [1 0 0 0 0], ...
    'HorizontalGap', 10*sz.pixf, ...
    'VerticalGap', 10*sz.pixf);

hLayout.add(h.listbox, [1 2], [1 4], ...
    'Fill', 'Both', ...
    'MinimumWidth', 0);

hLayout.add(h.moveup, 1, 5, ...
    'MinimumHeight', 20*sz.pixf, ...
    'MinimumWidth', 20*sz.pixf);

hLayout.add(h.movedown, 2, 5, ...
    'Anchor', 'North', ...
    'MinimumHeight', 20*sz.pixf, ...
    'MinimumWidth', 20*sz.pixf);

hLayout.add(h.currentfilter, 3, [1 5], ...
    'Fill',          'Horizontal', ...
    'TopInset',      -5*sz.pixf, ...
    'BottomInset',   -5*sz.pixf, ...
    'MinimumHeight', sz.uh, ...
    'MinimumWidth',  0)

hLayout.add(h.overwrite, 4, [1 5], ...
    'Fill',          'Horizontal', ...
    'TopInset',      -5*sz.pixf, ...
    'BottomInset',   -5*sz.pixf, ...
    'MinimumHeight', sz.uh, ...
    'MinimumWidth',  0)

hLayout.add(h.cascade, 5, 1, ...
    'MinimumHeight',  20*sz.pixf, ...
    'PreferredWidth', wd, ...
    'MinimumWidth',   20*sz.pixf, ...
    'Anchor',         'East');

hLayout.add(h.rename, 5, 2, ...
    'MinimumHeight', 20*sz.pixf, ...
    'MinimumWidth',  wd);

hLayout.add(h.delete, 5, 3, ...
    'MinimumHeight', 20*sz.pixf, ...
    'MinimumWidth',  wd);

hLayout.add(h.fvtool, 5, [4 5], ...
    'MinimumHeight',  20*sz.pixf, ...
    'PreferredWidth', wd, ...
    'MinimumWidth',   wd-10*sz.pixf, ...
    'Anchor',         'West');

set(this, 'Parent', hFig, 'Handles', h);

l = [ ...
        handle.listener(this, this.findprop('Overwrite'), ...
        'PropertyPostSet', @(h, ev) prop_listener(h, ev)); ...
        handle.listener(this, this.findprop('privSelectedFilters'), ...
        'PropertyPostSet', @selectedfilters_listener); ...
        handle.listener(this, 'NewFilter', @newfilter_listener); ...
        handle.listener(this, 'NewData',   @newdata_listener); ...
    ];
set(l, 'CallbackTarget', this);
set(this, 'WhenRenderedListeners', l);

newdata_listener(this);

set(this.FigureHandle, 'keypressfcn', {@keypress_cb, this});

% -------------------------------------------------------------------------
function keypress_cb(~, eventData, this)

if length(this.Data) ~= length(this.Selected)
    if any(strcmpi(eventData.Key, {'delete', 'backspace'}))
        removefilter(this);
    end
end

% -------------------------------------------------------------------------
function listbox_cb(hcbo, ~, this)

w = warning('off'); %#ok<WNOFF>

set(this, 'SelectedFilters', get(hcbo, 'Value'));
warning(w);
sendwarning(this);

% -------------------------------------------------------------------------
function newdata_listener(this, ~)

h = get(this, 'Handles');

names = getnames(this);
if isempty(names)
    names = {xlate('There are no stored filters.')};
end

set(h.listbox, 'String', names);

selectedfilters_listener(this);

% -------------------------------------------------------------------------
function selectedfilters_listener(this, ~)

% Make sure that the action buttons are enabled properly.
enable_listener(this);

h = get(this, 'Handles');

% Disable the string listener.
set(this.WhenRenderedListeners(1), 'Enable', 'Off');

set(h.listbox, 'Value', this.SelectedFilters);

newfilter_listener(this);

set(this.WhenRenderedListeners(1), 'Enable', 'On');

% -------------------------------------------------------------------------
function newfilter_listener(this, ~)

h = get(this, 'Handles');

names = getnames(this, this.CurrentFilter);
if isempty(names)
    enab = 'Off';
    names = {' '};
else
    enab = 'On';
end

set(h.currentfilter, 'String', sprintf('%s: %s', xlate('Current filter'), names{1}));
setenableprop(h.currentfilter, enab);

upenab = enab;
if any(this.SelectedFilters == 1)
    upenab = 'Off';
end

downenab = enab;
if any(this.SelectedFilters == length(this.Data))
    downenab = 'off';
end

setenableprop(h.movedown, downenab);
setenableprop(h.moveup,   upenab);

% -----------------------------------------------------------------------
function help_cb(hcbo, ~)

launchfv(ancestor(hcbo, 'figure'), 'fdatool_filtermanager_overview', 'fdatool');

% -------------------------------------------------------------------------
function close_cb(~, ~, this)

set(this, 'Visible', 'Off');

% [EOF]
